package com.lv.service.utils

import android.content.Context
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkRequest
import androidx.work.workDataOf
import com.blankj.utilcode.util.CollectionUtils
import com.google.android.material.tabs.TabLayout
import com.luck.picture.lib.entity.LocalMedia
import com.lv.common.utils.StringUtil
import com.lv.common.utils.TimeUtil
import com.lv.service.bean.ContactBean
import com.lv.service.bean.TelFourCategoryBean
import com.lv.service.bean.TelSecondCategory
import com.lv.service.bean.TelSecondCategoryBean
import com.lv.service.bean.TelThirdCategory
import com.lv.service.bean.TelThirdCategoryBean
import com.lv.service.room.address.AddressAllBean
import com.lv.service.room.address.AddressBean
import com.lv.service.room.address.AddressDbHelper
import com.lv.service.room.address.LocationNewReceiptAddressDbHelper
import com.lv.service.room.address.ReceiptAddressItem
import com.lv.service.room.card.CardAddressBean
import com.lv.service.room.card.CardAddressDbHelper
import com.lv.service.room.card.CardCategoryBean
import com.lv.service.room.card.CardCategoryDbHelper
import com.lv.service.room.card.CardColorBean
import com.lv.service.room.card.CardColorDbHelper
import com.lv.service.room.category.CategoryBean
import com.lv.service.room.category.CategoryDbHelper
import com.lv.service.room.category.NewTelCategoryBean
//import com.lv.service.room.category.NewTelCategoryCityBean
import com.lv.service.room.category.NewTelCategoryCityDbHelper
import com.lv.service.room.category.NewTelCategoryDbHelper
import com.lv.service.room.category.SpuCategoryBean
import com.lv.service.room.category.SpuCategoryDbHelper
import com.lv.service.room.saler.ProfessionalBean
import com.lv.service.room.saler.SalerDbHelper
import com.lv.service.room.saler.SuerBean
import com.lv.service.room.saler.SuerDbHelper
import com.lv.service.workmanager.LoadInfoWorker

/**
 * 作者： ygx
 * 创建日期：2023/2/11 2:49 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class YgxToolsKt {

    companion object {

        /**
         * 三级选择器数据初始化
         */
        fun initThirdCategory(
            mCategoryBeans: MutableList<CategoryBean>,
            fcname: Int?,
            secname: Int?,
            thcname: Int?
        ): Triple<String, String, String> {
            var mWheelPosition1 = 0
            var mWheelPosition2 = 0
            var mWheelPosition3 = 0
            for (i in 0 until mCategoryBeans.size) {
                val categoryBean = mCategoryBeans[i]
                if (fcname == categoryBean.fcaid) {
                    mWheelPosition1 = i
                    val telSecondCategories = categoryBean.telSecondCategories
                    if (CollectionUtils.isNotEmpty(telSecondCategories)) {
                        for (j in 0 until telSecondCategories!!.size) {
                            val telSecondCategory = telSecondCategories[j]
                            if (secname == telSecondCategory.secaid) {
                                mWheelPosition2 = j
                                val telThirdCategories = telSecondCategory.telThirdCategories
                                if (CollectionUtils.isNotEmpty(telThirdCategories)) {
                                    for (k in 0 until telThirdCategories!!.size) {
                                        val thirdCategory = telThirdCategories[k]
                                        if (thcname.toString() == thirdCategory.id) {
                                            mWheelPosition3 = k
                                            break
                                        }
                                    }
                                }
                                break
                            }
                        }
                    }
                    break
                }
            }
            return Triple(
                mWheelPosition1.toString(),
                mWheelPosition2.toString(),
                mWheelPosition3.toString()
            )
        }

        /**
         * 三级选择器数据初始化
         */
        fun initThirdAddress(
            mCategoryBeans: MutableList<CategoryBean>,
            fcname: String?,
            secname: String?,
            thcname: String?
        ): Triple<String, String, String> {
            var mWheelPosition1 = 0
            var mWheelPosition2 = 0
            var mWheelPosition3 = 0
            for (i in 0 until mCategoryBeans.size) {
                val categoryBean = mCategoryBeans[i]
                if (fcname == categoryBean.name) {
                    mWheelPosition1 = i
                    val telSecondCategories = categoryBean.telSecondCategories
                    if (CollectionUtils.isNotEmpty(telSecondCategories)) {
                        for (j in 0 until telSecondCategories!!.size) {
                            val telSecondCategory = telSecondCategories[j]
                            if (secname == telSecondCategory.name) {
                                mWheelPosition2 = j
                                val telThirdCategories = telSecondCategory.telThirdCategories
                                if (CollectionUtils.isNotEmpty(telThirdCategories)) {
                                    for (k in 0 until telThirdCategories!!.size) {
                                        val thirdCategory = telThirdCategories[k]
                                        if (thcname.toString() == thirdCategory.name) {
                                            mWheelPosition3 = k
                                            break
                                        }
                                    }
                                }
                                break
                            }
                        }
                    }
                    break
                }
            }
            return Triple(
                mWheelPosition1.toString(),
                mWheelPosition2.toString(),
                mWheelPosition3.toString()
            )
        }

        /**
         * 三级选择器数据初始化
         */
        fun initThirdAddressInt(
            mCategoryBeans: MutableList<CategoryBean>,
            fcname: String?,
            secname: String?,
            thcname: String?
        ): Triple<Int, Int, Int> {
            var mWheelPosition1 = 0
            var mWheelPosition2 = 0
            var mWheelPosition3 = 0
            for (i in 0 until mCategoryBeans.size) {
                val categoryBean = mCategoryBeans[i]
                if (fcname == categoryBean.name) {
                    mWheelPosition1 = i
                    val telSecondCategories = categoryBean.telSecondCategories
                    if (CollectionUtils.isNotEmpty(telSecondCategories)) {
                        for (j in 0 until telSecondCategories!!.size) {
                            val telSecondCategory = telSecondCategories[j]
                            if (secname == telSecondCategory.name) {
                                mWheelPosition2 = j
                                val telThirdCategories = telSecondCategory.telThirdCategories
                                if (CollectionUtils.isNotEmpty(telThirdCategories)) {
                                    for (k in 0 until telThirdCategories!!.size) {
                                        val thirdCategory = telThirdCategories[k]
                                        if (thcname.toString() == thirdCategory.name) {
                                            mWheelPosition3 = k
                                            break
                                        }
                                    }
                                }
                                break
                            }
                        }
                    }
                    break
                }
            }
            return Triple(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3
            )
        }

        /**
         * 为三级数据添加 全部
         */
        fun thirdSelectedAddAll(datas: List<AddressAllBean>): MutableList<CategoryBean> {
            val mAddressBeans = mutableListOf<CategoryBean>()
            val telThirdCas: MutableList<TelThirdCategory> = ArrayList()
            telThirdCas.clear()
            telThirdCas.add(TelThirdCategory("", "", "", "全部", 0, 0, false))
            val telSecondCats: MutableList<TelSecondCategory> = ArrayList()
            telSecondCats.clear()
            telSecondCats.add(
                TelSecondCategory(
                    0,
                    "",
                    "",
                    "全部",
                    0,
                    0,
                    telThirdCas,
                    false
                )
            )
            val category = CategoryBean(0, 0, "", "", "全部", "", 0, telSecondCats, false)
            mAddressBeans.add(0, category)
            for (element in datas) {
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategory>()
                val telThirdCategories: MutableList<TelThirdCategory> = ArrayList()
                telThirdCategories.clear()
                telThirdCategories.add(TelThirdCategory("", "", "", "全部", 0, 0, false))
                seconds.add(
                    TelSecondCategory(0, "", "", "全部", 0, 0, telThirdCategories, false)
                )
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategory>()
                        telThirds.add(TelThirdCategory("", "", "", "全部", 0, 0, false))
                        telThirdBeans?.let {
                            telThirds.addAll(telThirdBeans)
                        }
                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = CategoryBean(
                    element._id,
                    element.fcaid,
                    element.id, "",
                    element.image,
                    element.name,
                    element.seq,
                    seconds,
                    element.isSelcted,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }

        /**
         * 为四级数据添加 全部
         */
        fun fourSelectedAddAll(datas: List<ReceiptAddressItem>): MutableList<ReceiptAddressItem> {
            val mAddressBeans = mutableListOf<ReceiptAddressItem>()

            val telFourCas: MutableList<TelFourCategoryBean> = ArrayList()
            telFourCas.clear()
            telFourCas.add(TelFourCategoryBean("", "全部", "", "", "", 0, false))

            val telThirdCas: MutableList<TelThirdCategoryBean> = ArrayList()
            telThirdCas.clear()
            telThirdCas.add(TelThirdCategoryBean("", "全部", "", "", "0", 0, "", false, telFourCas))

            val telSecondCats: MutableList<TelSecondCategoryBean> = ArrayList()
            telSecondCats.clear()
            telSecondCats.add(
                TelSecondCategoryBean(
                    "",
                    "全部",
                    "",
                    "",
                    "",
                    0, "",
                    false,
                    telThirdCas
                )
            )
            val category =
                ReceiptAddressItem(0, "", "全部", "", "", "", 0, "", false, telSecondCats)
            mAddressBeans.add(0, category)
            for (element in datas) {
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategoryBean>()
                val telThirdCategories: MutableList<TelThirdCategoryBean> = ArrayList()
                telThirdCategories.clear()
                telThirdCategories.add(
                    TelThirdCategoryBean(
                        "",
                        "全部",
                        "",
                        "",
                        "",
                        0, "",
                        false,
                        telFourCas
                    )
                )
                seconds.add(
                    TelSecondCategoryBean("", "全部", "", "", "", 0, "", false, telThirdCategories)
                )
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategoryBean>()
                        telThirds.add(
                            TelThirdCategoryBean(
                                "",
                                "全部",
                                "",
                                "",
                                "",
                                0, "",
                                false,
                                telFourCas
                            )
                        )
                        telThirdBeans?.let {
                            for (k in telThirdBeans.indices) {
                                val telThirdCategoryBeans = telThirdBeans[k]
                                val telFourCategories = telThirdCategoryBeans.telFourCategories
                                val telFourChilds = mutableListOf<TelFourCategoryBean>()
                                telFourChilds.add(
                                    TelFourCategoryBean(
                                        "",
                                        "全部",
                                        "",
                                        "",
                                        "",
                                        0,
                                        false
                                    )
                                )
                                telFourCategories?.let {
                                    telFourChilds.addAll(telFourCategories)
                                }
                                telThirdCategoryBeans.telFourCategories = telFourChilds
                                telThirds.add(telThirdCategoryBeans)
                            }
                        }
                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = ReceiptAddressItem(
                    element._id,
                    element.id,
                    element.name,
                    element.firstChar,
                    element.forshort,
                    element.pforshort,
                    0, element.centre,
                    element.isShow,
                    seconds,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }

        /**
         * 为四级数据添加 全部
         */
        fun fourSelectedAddAllToCity(
            datas: List<ReceiptAddressItem>,
            allText: String = "全部"
        ): MutableList<ReceiptAddressItem> {
            val mAddressBeans = mutableListOf<ReceiptAddressItem>()

            val telFourCas: MutableList<TelFourCategoryBean> = ArrayList()
            telFourCas.clear()

            val telThirdCas: MutableList<TelThirdCategoryBean> = ArrayList()
            telThirdCas.clear()

            val telSecondCats: MutableList<TelSecondCategoryBean> = ArrayList()
            telSecondCats.clear()
            telSecondCats.add(
                TelSecondCategoryBean(
                    "",
                    allText,
                    "",
                    "",
                    "",
                    0, "",
                    false,
                    telThirdCas
                )
            )
//            val category = ReceiptAddressItem(0, "", "全部", "", "", "", 0, "",false, telSecondCats)
//            mAddressBeans.add(0, category)
            for (element in datas) {
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategoryBean>()
                val telThirdCategories: MutableList<TelThirdCategoryBean> = ArrayList()
                telThirdCategories.clear()

                seconds.add(
                    TelSecondCategoryBean("", allText, "", "", "", 0, "", false, telThirdCategories)
                )
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategoryBean>()

                        telThirdBeans?.let {
                            for (k in telThirdBeans.indices) {
                                val telThirdCategoryBeans = telThirdBeans[k]
                                val telFourCategories = telThirdCategoryBeans.telFourCategories
                                val telFourChilds = mutableListOf<TelFourCategoryBean>()

                                telFourCategories?.let {
                                    telFourChilds.addAll(telFourCategories)
                                }
                                telThirdCategoryBeans.telFourCategories = telFourChilds
                                telThirds.add(telThirdCategoryBeans)
                            }
                        }
                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = ReceiptAddressItem(
                    element._id,
                    element.id,
                    element.name,
                    element.firstChar,
                    element.forshort,
                    element.pforshort,
                    0, element.centre,
                    element.isShow,
                    seconds,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }

        /**
         * 处理四级数据 无 全部
         * 用于底部选择
         */
        fun fourSelectedAddress(datas: List<ReceiptAddressItem>): MutableList<ReceiptAddressItem> {
            val mAddressBeans = mutableListOf<ReceiptAddressItem>()
            for (element in datas) {
//                val previnceName = element.name ?: "全部"
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategoryBean>()
                val telThirdCategories: MutableList<TelThirdCategoryBean> = ArrayList()
                telThirdCategories.clear()
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategoryBean>()
                        telThirdBeans?.let {
                            for (k in telThirdBeans.indices) {
                                val telThirdCategoryBeans = telThirdBeans[k]
                                val telFourCategories = telThirdCategoryBeans.telFourCategories
                                val telFourChilds = mutableListOf<TelFourCategoryBean>()
                                telFourCategories?.let {
                                    telFourChilds.addAll(telFourCategories)
                                }
//                                if (previnceName.contains("海南省") || "海南省".contains(previnceName)) {
//                                    telFourCategories?.let {
//                                        telFourChilds.addAll(telFourCategories)
//                                    }
//                                } else {
//                                    telFourChilds.add(
//                                        TelFourCategoryBean(
//                                            "",
//                                            "全部",
//                                            "",
//                                            "",
//                                            "",
//                                            0,
//                                            false
//                                        )
//                                    )
//                                }

                                telThirdCategoryBeans.telFourCategories = telFourChilds
                                telThirds.add(telThirdCategoryBeans)
                            }
                        }
                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = ReceiptAddressItem(
                    element._id,
                    element.id,
                    element.name,
                    element.firstChar,
                    element.forshort,
                    element.pforshort,
                    0, element.centre,
                    element.isShow,
                    seconds,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }

        /**
         * 为四级数据添加 全部
         * 除海南，其它没有四级
         * 用于顶部搜索
         */
        fun fourSelectedAddAllNoFour(datas: List<ReceiptAddressItem>): MutableList<ReceiptAddressItem> {
            val mAddressBeans = mutableListOf<ReceiptAddressItem>()

            val telFourCas: MutableList<TelFourCategoryBean> = ArrayList()
            telFourCas.clear()
            telFourCas.add(TelFourCategoryBean("", "全部", "", "", "", 0, false))

            val telThirdCas: MutableList<TelThirdCategoryBean> = ArrayList()
            telThirdCas.clear()
            telThirdCas.add(TelThirdCategoryBean("", "全部", "", "", "0", 0, "", false, telFourCas))

            val telSecondCats: MutableList<TelSecondCategoryBean> = ArrayList()
            telSecondCats.clear()
            telSecondCats.add(
                TelSecondCategoryBean(
                    "",
                    "全部",
                    "",
                    "",
                    "",
                    0, "",
                    false,
                    telThirdCas
                )
            )
            val category =
                ReceiptAddressItem(0, "", "全部", "", "", "", 0, "", false, telSecondCats)
            mAddressBeans.add(0, category)
            for (element in datas) {
//                val previnceName = element.name ?: "全部"
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategoryBean>()
                val telThirdCategories: MutableList<TelThirdCategoryBean> = ArrayList()
                telThirdCategories.clear()
                telThirdCategories.add(
                    TelThirdCategoryBean(
                        "",
                        "全部",
                        "",
                        "",
                        "",
                        0, "",
                        false,
                        telFourCas
                    )
                )
                seconds.add(
                    TelSecondCategoryBean("", "全部", "", "", "", 0, "", false, telThirdCategories)
                )
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategoryBean>()
                        telThirds.add(
                            TelThirdCategoryBean(
                                "",
                                "全部",
                                "",
                                "",
                                "",
                                0, "",
                                false,
                                telFourCas
                            )
                        )
                        telThirdBeans?.let {
                            for (k in telThirdBeans.indices) {
                                val telThirdCategoryBeans = telThirdBeans[k]
                                val telFourCategories = telThirdCategoryBeans.telFourCategories
                                val telFourChilds = mutableListOf<TelFourCategoryBean>()
                                telFourChilds.add(
                                    TelFourCategoryBean(
                                        "",
                                        "全部",
                                        "",
                                        "",
                                        "",
                                        0,
                                        false
                                    )
                                )
//                                if (previnceName.contains("海南省") || "海南省".contains(previnceName)) {
                                telFourCategories?.let {
                                    telFourChilds.addAll(telFourCategories)
                                }
//                                }

                                telThirdCategoryBeans.telFourCategories = telFourChilds
                                telThirds.add(telThirdCategoryBeans)
                            }
                        }
                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = ReceiptAddressItem(
                    element._id,
                    element.id,
                    element.name,
                    element.firstChar,
                    element.forshort,
                    element.pforshort,
                    0, "",
                    element.isShow,
                    seconds,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }


        fun getLocalMedia(url: String): LocalMedia {
            val fristMedia = LocalMedia()
            fristMedia.compressPath = url
            fristMedia.realPath = url
            fristMedia.path = url
            return fristMedia
        }

        /**
         * 设置tab选中的文字加粗
         */
        fun setSelectedBoldTab(): TabLayout.OnTabSelectedListener {
            return object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        val selecTab = text.toString().trim()
                        val spannedString = setTextSpan(selecTab, Typeface.BOLD)
                        tab.text = spannedString
                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        val unSelecTab = text.toString().trim()
                        val spannedString = setTextSpan(unSelecTab)
                        tab.text = spannedString
                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            }
        }

        /**
         * string 转 SpannableString
         */
        fun setTextSpan(selecTab: String, typeface: Int = Typeface.NORMAL): SpannableString {
            val spannedString = SpannableString(selecTab)
            val styleSpan = StyleSpan(typeface)
            spannedString.setSpan(
                styleSpan,
                0,
                selecTab.length,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )
            return spannedString
        }

        fun initSelectedTime(mYear: Int, mMonth: Int, mDay: Int): MutableList<CategoryBean> {
            val mTimeBeans = mutableListOf<CategoryBean>()
            for (i in 2018 until (mYear + 1)) {
                var mM = 12
                if (i == mYear) {
                    mM = mMonth
                }
                val telSecondCategory = mutableListOf<TelSecondCategory>()
                val thirdFriss = mutableListOf<TelThirdCategory>()
                thirdFriss.add(TelThirdCategory("", "", "", "全部", 0, 0, false, false))
                val secondFrist =
                    TelSecondCategory(0, "", "", "全部", 0, 0, thirdFriss, false, false)
                telSecondCategory.add(secondFrist)
                for (m in 1 until (mM + 1)) {
                    val thirdCategory = mutableListOf<TelThirdCategory>()
                    val thirdFirst = TelThirdCategory("", "", "", "全部", 0, 0, false, false)
                    thirdCategory.add(thirdFirst)
                    val mouthCure = String.format("%02d", m)
                    var dM = TimeUtil.getDaysByDate("${i}-${mouthCure}")
                    if (i == mYear && m == mMonth) {
                        dM = mDay
                    }
                    for (d in 1 until (dM + 1)) {
                        val dayCure = String.format("%02d", d)
                        val third = TelThirdCategory("", "", "", "${dayCure}日", 0, 0, false)
                        thirdCategory.add(third)
                    }
                    val second =
                        TelSecondCategory(0, "", "", "${mouthCure}月", 0, 0, thirdCategory, false)
                    telSecondCategory.add(second)
                }
                val categoryBean = CategoryBean(
                    0,
                    0,
                    "",
                    "", "",
                    "${i}年",
                    0,
                    telSecondCategory,
                    false,
                )
                mTimeBeans.add(categoryBean)
            }
            return mTimeBeans
        }


        fun initSelectedNoAllTime(
            mYear: Int,
            mMonth: Int,
            mDay: Int,
            mDistance: Int = 5,
            showUnit: Boolean = true
        ): MutableList<CategoryBean> {
            val mTimeBeans = mutableListOf<CategoryBean>()
            val mDistanceY = mYear - mDistance
            for (i in mDistanceY until (mYear + 1)) {
                var mM = 12
                if (i == mYear) {
                    mM = mMonth
                }
                val telSecondCategory = mutableListOf<TelSecondCategory>()
//                val thirdFriss = mutableListOf<TelThirdCategory>()
//                thirdFriss.add(TelThirdCategory("", "", "", "全部", 0, 0, false, false))
//                val secondFrist = TelSecondCategory(0, "", "", "全部", 0, 0, thirdFriss, false, false)
//                telSecondCategory.add(secondFrist)
                for (m in 1 until (mM + 1)) {
                    val thirdCategory = mutableListOf<TelThirdCategory>()
//                    val thirdFirst = TelThirdCategory("", "", "", "全部", 0, 0, false, false)
//                    thirdCategory.add(thirdFirst)
                    val mouthCure = String.format("%02d", m)
                    var dM = TimeUtil.getDaysByDate("${i}-${mouthCure}")
                    if (i == mYear && m == mMonth) {
                        dM = mDay
                    }
                    for (d in 1 until (dM + 1)) {
                        val dayCure = String.format("%02d", d)
                        val third = TelThirdCategory(
                            "",
                            "",
                            "",
                            if (showUnit) "${dayCure}日" else dayCure,
                            0,
                            0,
                            false
                        )
                        thirdCategory.add(third)
                    }
                    val second =
                        TelSecondCategory(
                            0,
                            "",
                            "",
                            if (showUnit) "${mouthCure}月" else mouthCure,
                            0,
                            0,
                            thirdCategory,
                            false
                        )
                    telSecondCategory.add(second)
                }
                val categoryBean = CategoryBean(
                    0,
                    0,
                    "",
                    "", "",
                    if (showUnit) "${i}年" else "$i",
                    0,
                    telSecondCategory,
                    false,
                )
                mTimeBeans.add(categoryBean)
            }
            return mTimeBeans
        }

        //指定起始结束时间的时间数据源
        fun initSelectedStaEndTime(
            mStart: String, mEnd: String,
            showUnit: Boolean = true
        ): MutableList<CategoryBean> {
            val startSplit = mStart.split("-")
            val startYear = startSplit[0].toInt()
            val startMonth = startSplit[1].toInt()
            val startDays = startSplit[2].toInt()

            val endSplit = mEnd.split("-")
            val endYear = endSplit[0].toInt()
            val endMonth = endSplit[1].toInt()
            val endDays = endSplit[2].toInt()
            val mTimeBeans = mutableListOf<CategoryBean>()
            for (i in startYear until (endYear + 1)) {
                var stM = 1
                if (i == startYear) {
                    stM = startMonth
                }
                var endM = 12
                if (i == endYear) {
                    endM = endMonth
                }
                val telSecondCategory = mutableListOf<TelSecondCategory>()
//                val thirdFriss = mutableListOf<TelThirdCategory>()
//                thirdFriss.add(TelThirdCategory("", "", "", "全部", 0, 0, false, false))
//                val secondFrist = TelSecondCategory(0, "", "", "全部", 0, 0, thirdFriss, false, false)
//                telSecondCategory.add(secondFrist)
                for (m in stM until (endM + 1)) {
                    val thirdCategory = mutableListOf<TelThirdCategory>()
//                    val thirdFirst = TelThirdCategory("", "", "", "全部", 0, 0, false, false)
//                    thirdCategory.add(thirdFirst)
                    val mouthCure = String.format("%02d", m)
                    var dSt = 1
                    if (i == startYear && m == startMonth) {
                        dSt = startDays
                    }
                    var dend = 1
                    if (i == endYear && m == endMonth) {
                        dend = endDays
                    } else {
                        dend = TimeUtil.getDaysByDate("$endYear-$mouthCure")
                    }

                    for (d in dSt until (dend + 1)) {
                        val dayCure = String.format("%02d", d)
                        val third = TelThirdCategory(
                            "", "", "",
                            if (showUnit) "${dayCure}日" else dayCure,
                            0, 0, false
                        )
                        thirdCategory.add(third)
                    }
                    val second =
                        TelSecondCategory(
                            0,
                            "",
                            "",
                            if (showUnit) "${mouthCure}月" else mouthCure,
                            0,
                            0,
                            thirdCategory,
                            false
                        )
                    telSecondCategory.add(second)
                }
                val categoryBean = CategoryBean(
                    0,
                    0,
                    "",
                    "", "",
                    if (showUnit) "${i}年" else "$i",
                    0,
                    telSecondCategory,
                    false,
                )
                mTimeBeans.add(categoryBean)
            }
            return mTimeBeans
        }

        /**
         * 省份期数选择
         * @param mM 期数
         */
        fun initProvincePeroids(datas: List<String>, mM: Int = 25): MutableList<CategoryBean> {
            val mTimeBeans = mutableListOf<CategoryBean>()
            for (i in 0 until datas.size) {
                val telSecondCategory = mutableListOf<TelSecondCategory>()
                val thirdFriss = mutableListOf<TelThirdCategory>()
                thirdFriss.add(TelThirdCategory("", "", "", "全部", 0, 0, false, false))
                val secondFrist =
                    TelSecondCategory(0, "", "", "全部", 0, 0, thirdFriss, false, false)
                telSecondCategory.add(secondFrist)
                for (m in 1 until (mM + 1)) {
                    val thirdCategory = mutableListOf<TelThirdCategory>()
                    val thirdFirst = TelThirdCategory("", "", "", "全部", 0, 0, false, false)
                    thirdCategory.add(thirdFirst)
                    val mouthCure = String.format("%02d", m)
                    val second =
                        TelSecondCategory(0, "", "", "${mouthCure}期", 0, 0, thirdCategory, false)
                    telSecondCategory.add(second)
                }
                val categoryBean = CategoryBean(
                    0,
                    0,
                    "",
                    "", "",
                    "${datas[i]}",
                    0,
                    telSecondCategory,
                    false,
                )
                mTimeBeans.add(categoryBean)
            }
            return mTimeBeans
        }

        /**
         * 省份期数选择
         * @param mM 期数
         */
        fun initProvincePeroidsNotAll(
            datas: List<String>,
            mM: Int = 25
        ): MutableList<CategoryBean> {
            val mTimeBeans = mutableListOf<CategoryBean>()
            for (i in 0 until datas.size) {
                val telSecondCategory = mutableListOf<TelSecondCategory>()
//                val thirdFriss = mutableListOf<TelThirdCategory>()
//                thirdFriss.add(TelThirdCategory("", "", "", "全部", 0, 0, false, false))
//                val secondFrist = TelSecondCategory(0, "", "", "全部", 0, 0, thirdFriss, false, false)
//                telSecondCategory.add(secondFrist)
                for (m in 1 until (mM + 1)) {
                    val thirdCategory = mutableListOf<TelThirdCategory>()
//                    val thirdFirst = TelThirdCategory("", "", "", "全部", 0, 0, false, false)
//                    thirdCategory.add(thirdFirst)
                    val mouthCure = String.format("%02d", m)
                    val second =
                        TelSecondCategory(0, "", "", "${mouthCure}期", 0, 0, thirdCategory, false)
                    telSecondCategory.add(second)
                }
                val categoryBean = CategoryBean(
                    0,
                    0,
                    "",
                    "", "",
                    "${datas[i]}",
                    0,
                    telSecondCategory,
                    false,
                )
                mTimeBeans.add(categoryBean)
            }
            return mTimeBeans
        }

        /* fun findProsNewReceiptAddress(
             context: Context,
             viewModuleService: ServiceViewModel,
             myScope: CoroutineScope,
             lifecycleScope: LifecycleCoroutineScope
         ) {
             val map = mutableMapOf<String, String>(
                 "newreceiptAddress" to "1",
             )
             lifecycleScope.launchWhenCreated {
                 this.launch {
                     val result: DataResult<BaseYgxRsp> =
                         myScope.async { viewModuleService.findPros(map) }.await()
                     parseFindProsResult(context, result, lifecycleScope)
                 }
             }
         }

         private fun parseFindProsResult(
             context: Context,
             data: DataResult<BaseYgxRsp>,
             lifecycleScope: LifecycleCoroutineScope
         ) {
             data.onSuccess {
                 onBizOK<FindProsBean> { code, data, message, result ->
                     data?.let {

                         lifecycleScope.launch {

                             //四级地址选择器数据
                             this.launch {
                                 val addressList = it.newreceiptAddress
                                 if (addressList != null) {
                                     saveNewReceiptAddressAllRoom(context, addressList)
                                 }
                             }


                         }
                     }

                 }
                 onBizError<FindProsBean> { code, message, data ->
                     showToast(message)
                 }
             }
             data.onFailure {
                 LogUtils.e("eee")
             }
         }*/

        /**
         * 存地址到数据库
         */
        suspend fun saveNewReceiptAddressAllRoom(
            context: Context,
            addressList: List<ReceiptAddressItem>,
        ) {
//            LocationNewReceiptAddressDbHelper.deleteAllLocation(context)
            val addressBeans = mutableListOf<ReceiptAddressItem>()
            for ((index, e) in addressList.withIndex()) {
                val addressBean = ReceiptAddressItem(
                    index,
                    e.id,
                    e.name,
                    e.firstChar,
                    e.forshort,
                    e.pforshort,
                    e.seq, e.centre,
                    e.isShow,
                    e.telSecondCategories,
                )
                addressBeans.add(addressBean)
            }
            LocationNewReceiptAddressDbHelper.insertAllLocation(
                context,
                addressBeans
            )
        }

        /**
         * 存分类到数据库
         */
        suspend fun saveRoomCategory(context: Context, categoryList: List<CategoryBean>) {
            val categoryBeans = mutableListOf<CategoryBean>()
            for ((index, e) in categoryList.withIndex()) {
                val categoryBean = CategoryBean(
                    index,
                    e.fcaid,
                    e.id, e.forshort,
                    e.image,
                    e.name,
                    e.seq,
                    e.telSecondCategories,
                    e.isSelcted
                )
                categoryBeans.add(categoryBean)
            }
            CategoryDbHelper.insertAllCategory(
                context,
                categoryBeans
            )
        }

        /**
         * 存找货大全分类到数据库
         */
        suspend fun saveCardCategory(context: Context, categoryList: List<CardCategoryBean>) {
//            CardCategoryDbHelper.deleteSuspendAllCategory(context)
            val categoryBeans = mutableListOf<CardCategoryBean>()
            for ((index, e) in categoryList.withIndex()) {
                val categoryBean = CardCategoryBean(
                    index,
                    e.fcaid,
                    e.id,
                    e.image,
                    e.name,
                    e.seq,
                    e.telSecondCategories,
                    e.isSelcted
                )
                categoryBeans.add(categoryBean)
            }
            CardCategoryDbHelper.insertAllCategory(
                context,
                categoryBeans
            )
        }

        /**
         * 存商品分类到数据库
         */
        suspend fun saveGoodsCategory(context: Context, categoryList: List<CardCategoryBean>) {
            val categoryBeans = mutableListOf<SpuCategoryBean>()
            for ((index, e) in categoryList.withIndex()) {
                val categoryBean = SpuCategoryBean(
                    index,
                    e.fcaid,
                    e.id,
                    e.image,
                    e.name,
                    e.seq,
                    e.telSecondCategories,
                    e.isSelcted
                )
                categoryBeans.add(categoryBean)
            }
            SpuCategoryDbHelper.insertAllCategory(
                context,
                categoryBeans
            )
        }

        /**
         * 存名片地址到数据库
         */
        suspend fun saveCardAddress(context: Context, addressList: List<CardAddressBean>) {
            val addressBeans = mutableListOf<CardAddressBean>()
            for ((index, e) in addressList.withIndex()) {
                val addressBean = CardAddressBean(
                    index,
                    e.fcaid,
                    e.id,
                    e.image,
                    e.name,
                    e.seq,
                    e.telSecondCategories,
                    e.isSelcted,
                )
                addressBeans.add(addressBean)
            }
            CardAddressDbHelper.insertFindSuspendCategory(
                context,
                addressBeans
            )
        }

        /**
         * 存名片信息到数据库
         */
        suspend fun saveCardColorInfo(context: Context, cardColorList: List<CardColorBean>) {
            val cardColorBeans = mutableListOf<CardColorBean>()
            for ((index, e) in cardColorList.withIndex()) {
                val cardColorBean = CardColorBean(
                    index,
                    e.id,
                    e.enterpage,
                    e.buypage,
                    e.listpage,
                    e.cardpage,
                    e.newcardpage
                )
                cardColorBeans.add(cardColorBean)
            }
            CardColorDbHelper.insertAllCardColor(
                context,
                cardColorBeans
            )
        }

        /**
         * 存业务员信息到数据库
         */
        suspend fun saveSalerInfos(context: Context, professionalBeans: List<ProfessionalBean>) {
//            SalerDbHelper.deleteAllSaler(context)
            val professionals = mutableListOf<ProfessionalBean>()
            for ((index, e) in professionalBeans.withIndex()) {
                if (e.role.contains("D") || e.role.contains("E") || e.role.contains("F")) {
                    val professionalBean = ProfessionalBean(
                        index,
                        e.password,
                        e.id,
                        e.created,
                        e.updated,
                        e.lastlogin,
                        e.logo,
                        e.name,
                        e.nickname,
                        e.txaccid,
                        e.role,
                        e.phone,
                        e.issaler,
                        e.txaccidpassword,
                        e.head,
                        e.headid,
                        e.headphone,
                        e.bpro,
                        e.book,
                        e.invitecode,
                        e.invitecodeurl,
                        StringUtil.getSpells(e.name.first().toString())
                            .uppercase(),
                        0
                    )
                    professionals.add(professionalBean)
                }
            }
            SalerDbHelper.insertAllSaler(context, professionals)

        }

        /**
         * 存发行人员信息到数据库
         */
        suspend fun saveSuerInfos(context: Context, suerBeans: List<ProfessionalBean>) {
//            SuerDbHelper.deleteAllSaler(context)
            val professionals = mutableListOf<SuerBean>()
            for ((index, e) in suerBeans.withIndex()) {
                if (e.role.contains("B") || e.role.contains("C")) {
                    val professionalBean = SuerBean(
                        index,
                        e.password,
                        e.id,
                        e.created,
                        e.updated,
                        e.lastlogin,
                        e.logo,
                        e.name,
                        e.nickname,
                        e.txaccid,
                        e.role,
                        e.phone,
                        e.issaler,
                        e.txaccidpassword,
                        e.head,
                        e.headid,
                        e.headphone,
                        e.bpro,
                        e.book,
                        e.invitecode,
                        e.invitecodeurl,
                        StringUtil.getSpells(e.name.first().toString())
                            .uppercase(),
                        0
                    )
                    professionals.add(professionalBean)
                }
            }
            SuerDbHelper.insertAllSaler(context, professionals)


        }

        /**
         * 存地址到数据库
         */
        suspend fun saveAddressRoom(context: Context, addressList: List<CategoryBean>) {
            val addressBeans = mutableListOf<AddressBean>()
            for ((index, e) in addressList.withIndex()) {
                val telSecondCategories = e.telSecondCategories
                val size = telSecondCategories?.size ?: 0
                if (size > 0) {
                    for (i in 0 until telSecondCategories!!.size) {
                        telSecondCategories[i].image = e.name
                    }
                }
                val addressBean = AddressBean(
                    index, e.id, e.image ?: "", e.name, telSecondCategories, e.isSelcted
                )
                addressBeans.add(addressBean)
            }
            AddressDbHelper.insertFindSuspendCategory(
                context,
                addressBeans
            )
        }

        /**
         * 存新分类数据到数据库
         */
        suspend fun saveNewTelCategoryRoom(
            context: Context,
            newTelCategoryBean: List<NewTelCategoryBean>
        ) {
//            val addressBeans = mutableListOf<NewTelCategoryBean>()
//            for ((index, e) in newTelCategoryBean.withIndex()) {
//                val addressBean = NewTelCategoryBean(
//                    index, e.id, e.fcaid, e.name, e.seq, e.image, e.isSelcted
//                )
//                addressBeans.add(addressBean)
//            }
            NewTelCategoryDbHelper.insertAllCategory(
                context, newTelCategoryBean
            )
        }

        /**
         * 存新分类数据到数据库
         */
        suspend fun saveNewTelCategoryCityRoom(
            context: Context,
            newTelCategoryBean: List<NewTelCategoryBean>
        ) {
            NewTelCategoryCityDbHelper.insertAllCategory(
                context, newTelCategoryBean
            )
        }

        /**
         * 存地址到数据库
         */
        /* suspend fun saveAddressAllRoom(
            context: Context,
            addressList: List<CategoryBean>,
        ) {
            AddressAllDbHelper.deleteSuspendAllAddress(context)
            val addressBeans = mutableListOf<AddressAllBean>()
            for ((index, e) in addressList.withIndex()) {
                val telSecondCategories = e.telSecondCategories
                val size = telSecondCategories?.size ?: 0
                if (size > 0) {
                    for (i in 0 until telSecondCategories!!.size) {
                        telSecondCategories[i].image = e.name
                    }
                }
                val addressBean = AddressAllBean(
                    index,
                    e.fcaid,
                    e.id,
                    e.image,
                    e.name,
                    e.seq,
                    telSecondCategories,
                    e.isSelcted
                )
                addressBeans.add(addressBean)
            }
            AddressAllDbHelper.insertFindSuspendCategory(
                context,
                addressBeans
            )
        }*/


        /**
         * 执行后台任务
         */
        fun doWorkManager(context: Context, isRefresh: String = "-1") {
            val upLoadInfoWorkerRequest: WorkRequest =
                OneTimeWorkRequestBuilder<LoadInfoWorker>()
                    .setInputData(workDataOf("name" to isRefresh))
                    .build()
            WorkManager.getInstance(context).enqueue(upLoadInfoWorkerRequest)
        }

        fun getPeriod(period: String): String {
            var period1 = period
            if (period1.startsWith("0")) {
                period1 = period1.drop(1)
            }
            if (period1.endsWith("期")) {
                period1 = period1.dropLast(1)
            }
            period1 = "${period1.toFloat().toInt()}"
            return period1
        }

        /**
         * 水印重复过滤
         */
        fun watermarkingFilter(image: String?, watermarking: String?): String {
            var mWatermarking = image ?: ""
            if (!watermarking.isNullOrEmpty()) {
                if (mWatermarking.contains("?")) {
                    val url = mWatermarking.split("?")
                    mWatermarking = "${url[0]}$watermarking"
                } else {
                    mWatermarking = "$mWatermarking$watermarking"
                }
            }
            return mWatermarking
        }

        /**
         * 水印过滤
         */
        fun watermarkingDelete(image: String?, watermarking: String?): String {
            var mWatermarking = image ?: ""
            if (!watermarking.isNullOrEmpty()) {
                if (mWatermarking.contains("?")) {
                    val url = mWatermarking.split("?")
                    mWatermarking = url[0]
                }
            }
            return mWatermarking
        }

        fun getCompanyTel(companyTel: String?, contact: List<ContactBean>?): String {
            var tel = companyTel
            if (!contact.isNullOrEmpty()) {
                tel = contact[0].tel
            }
            if (tel.isNullOrEmpty()) {
                tel = companyTel
            }
            return tel ?: ""
        }

        /**
         * 是否是省会城市
         */
        fun isCenterForProvince(cityName: String): Boolean {
            var city = cityName
            if (city.endsWith("市")) {
                city = cityName.dropLast(1)
            }
            // 省会城市列表
            val provincialCapitals = setOf(
                "北京",
                "天津",
                "石家庄",
                "太原",
                "呼和浩特",
                "沈阳",
                "长春",
                "哈尔滨",
                "上海",
                "南京",
                "杭州",
                "合肥",
                "福州",
                "南昌",
                "济南",
                "郑州",
                "武汉",
                "长沙",
                "佛山",
                "南宁",
                "海口",
                "重庆",
                "成都",
                "贵阳",
                "昆明",
                "拉萨",
                "西安",
                "兰州",
                "西宁",
                "银川",
                "乌鲁木齐",
                "台北",
                "香港",
                "澳门",
            )
            return city in provincialCapitals
        }

    }
}